package org.aspectsense.middleware.monitor;

import java.util.logging.Logger;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;

/**
 * Implements the OSGi <code>ServiceListener</code> for listening to service changes.<p>
 * Whenever a new <code>ServiceEvent</code> is generated it overrides the
 * serviceChanged method to catch and manage received service events.
 */

	public class DefaultServiceListener implements ServiceListener
	{
	    private static final Logger logger = Logger.getLogger(DefaultServiceListener.class.getCanonicalName());
	    
	    /**
	     * Overrides the OSGi <code>serviceChanged</code> method
	     * in order to catch and manages service events.
	     * @param serviceEvent The service monitored by OSGi
	     *
	     */
	    public void serviceChanged(ServiceEvent serviceEvent)
	    {
	        final int serviceEventType = serviceEvent.getType();
	       

	        switch (serviceEventType)
	        {
	            case ServiceEvent.REGISTERED:{
	            	 handleRegisteredService(serviceEvent.getServiceReference());
	            	
		                break;
	            }
	               
	            case ServiceEvent.MODIFIED:
	                handleModifiedService(serviceEvent.getServiceReference());
	                break;

	            case ServiceEvent.UNREGISTERING:
	                handleUnregisteringService(serviceEvent.getServiceReference());
	                break;
	        }
	    }
	    
	    /**
	     * The handleRegisteredService method is called when the {@link DefaultServiceListener }
	     * detects a service registration event
	     * @param serviceReference A reference to the service that has been registered
	     */
	    private void handleRegisteredService(ServiceReference serviceReference)
	    {
	    	logger.info("DefaultServiceListener::handleRegisteredService -> " + serviceReference.toString());
	    	Object serviceProvider = getServiceProvider(serviceReference);
	    	OSGiServiceEvent se = new OSGiServiceEvent (serviceProvider,serviceReference,OSGiServiceEvent.Action.REGISTERED);
	    	EventsManager.addEvent(se);
	    }

		/**
	     * Informs the Logger object about a service modification.
	     * @param serviceReference a reference to the modified service object.
	     */
	    private void handleModifiedService(ServiceReference serviceReference)
	    {
	        logger.info("DefaultServiceListener::handleModifiedService -> " + serviceReference.toString());
	    }
	    
	    /**
	     * Informs the Logger object about the unregistration of a service.
	     * @param serviceReference a reference to the unregistering service object.
	     */
	    private void handleUnregisteringService(ServiceReference serviceReference)
	    {
	       	logger.info("DefaultServiceListener::handleUnregisteringService -> " + serviceReference.toString());
	    	Object serviceProvider = getServiceProvider(serviceReference);
	    	OSGiServiceEvent se = new OSGiServiceEvent (serviceProvider,serviceReference,OSGiServiceEvent.Action.UNREGISTERED);
	    	EventsManager.addEvent(se);
	    }

	   
	    private Object getServiceProvider(final ServiceReference serviceReference)
	    {
	        final Bundle bundle = serviceReference.getBundle();
	        final BundleContext bundleContext = bundle.getBundleContext();
	        return bundleContext.getService(serviceReference);
	    }

	  
}

